from rest_framework import serializers
from django.contrib.auth.hashers import make_password,check_password
from utils.get_user_add import get_local
from makelove import models

"""
User序列化器
"""
class Userserializers(serializers.ModelSerializer):
    def create(self,data):
        data['password']=make_password(data['password'])
        u=models.User.objects.create(**data)
        return u
    class Meta:
        model=models.User
        fields='__all__'
        read_only_exclude=['password']  #我不希望在序列化时前端可以看到密码，排除掉password


class Followserializers(serializers.ModelSerializer):
    class Meta:
        model=models.Relation
        fields = ('user_one', 'user_two', 'follow')
    def create(self, value):
        follow_data=self.context
        f=models.Relation.objects.create(
            follow=follow_data['follow'],
            user_one=value['user_one'],
            user_two=value['user_two'],
        )
        return f
    def update(self, instance, data):
        instance.follow=data.get('follow',instance.follow)
        instance.save()
        return instance



